<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/Welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input v-model="queryInfo.query" placeholder="请输入内容" clearable @clear="getGoodsList">
          <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
        </el-input>
        </el-col>
        <el-col :span="4">
        <el-button type="primary" @click="goAddGoodPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表的表格区域 -->
      <el-table :data="goodsList" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index">
        </el-table-column>
        <el-table-column label="商品名称" prop="goods_name">
        </el-table-column>
         <el-table-column label="商品价格(元)" prop="goods_price" width="95px">
        </el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="70px">
        </el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="140px">
          <template slot-scope="scope">
            <!-- 使用自定义格式化时间的全局过滤器dateFormat，将时间戳形式的时间转换成yyyy-mm-dd hh:mm:ss形式的字符串时间 -->
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="showEditGood(scope.row)"></el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeGoodById(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 商品列表的导航区域 -->
       <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total" background>
      </el-pagination>
    </el-card>
    <!-- 编辑商品的对话框 -->
    <el-dialog
    title="编辑商品"
    :visible.sync="editCateDialogVisible"
    width="50%">
    <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px" label-position="left">
      <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
      </el-form-item>
      <el-form-item label="商品价格" prop="goods_price">
        <el-input v-model="editForm.goods_price" type="number"></el-input>
      </el-form-item>
      <el-form-item label="商品重量" prop="goods_weight">
        <el-input v-model="editForm.goods_weight" type="number"></el-input>
      </el-form-item>
      <el-form-item label="商品数量" prop="goods_number">
        <el-input v-model="editForm.goods_number" type="number"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="editCateDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="editGoodBtn">确 定</el-button>
    </span>
   </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      // 查询参数对象
      queryInfo: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 10
      },
      // 获取到的商品列表数组
      goodsList: [],
      // 商品列表的总商品数目
      total: 0,
      // 控制编辑商品对话框的显示与隐藏
      editCateDialogVisible: false,
      // 编辑商品的表单
      editForm: {
        // 商品的id
        goods_id: 0,
        // 商品名称
        goods_name: '',
        // 商品价格
        goods_price: 0,
        // 商品数量
        goods_number: 0,
        // 商品重量
        goods_weight: 0
      },
      // 表单的验证规则对象
      editFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    // 获取商品数据列表
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表数据
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败！')
      }
      this.$message.success('获取商品列表成功！')
      console.log(res.data)
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 点击按钮，显示编辑商品的对话框
    showEditGood (row) {
      console.log('编辑商品:' + JSON.stringify(row))
      // 在显示编辑对话框之前，将当前行的商品数据赋值给editForm表单对象
      this.editForm.goods_id = row.goods_id
      this.editForm.goods_name = row.goods_name
      this.editForm.goods_price = row.goods_price
      this.editForm.goods_number = row.goods_number
      this.editForm.goods_weight = row.goods_weight
      // 显示编辑商品的对话框
      this.editCateDialogVisible = true
    },
    // 编辑商品的表单提交事件触发函数
    editGoodBtn () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) {
          this.$message.error('编辑提交商品预验证失败！')
        }
        const { data: res } = await this.$http.put(`goods/${this.editForm.goods_id}`, this.editForm)
        console.log(res)
        if (res.meta.status !== 200) {
          this.$message.error('更新商品信息失败！')
        }
        this.$message.success('更新商品成功')
        // 隐藏编辑商品的对话框
        this.editCateDialogVisible = false
        // 刷新商品列表
        this.getGoodsList()
      })
    },
    // 点击按钮，删除对应的商品
    // 根据商品id，删除对应的商品
    removeGoodById (id) {
      console.log('删除商品 goods_id:' + id)
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 删除商品(API 1.8.5 删除商品)
        const { data: res } = await this.$http.delete(`goods/${id}`)
        if (res.meta.status !== 200) {
          return this.$message.error('删除商品失败！')
        }
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 重新刷新商品列表
        this.getGoodsList()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // pageSize 改变时会触发
    handleSizeChange (newPageSize) {
      // console.log(`每页${newPageSize}条`)
      this.queryInfo.pagesize = newPageSize
      // 重新获取商品列表，刷新列表
      this.getGoodsList()
    },
    // currentPage 改变时会触发
    handleCurrentChange (curenetPage) {
      // console.log(`当前页：${curenetPage}`)
      this.queryInfo.pagenum = curenetPage
      // 重新刷新商品列表
      this.getGoodsList()
    },
    goAddGoodPage () {
      this.$router.push('/goods/add')
    }
  }
}
</script>
<style lang="less" scoped>
</style>
